<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// ------------------------------------------------------------------------

/**
 * DiliCMS 后台权限控制类
 *
 * @package     DiliCMS
 * @subpackage  Libraries
 * @category    Libraries
 * @author      Jeongee
 * @link        http://www.dilicms.com
 */
class Acl_lib 
{
	private $CI = NULL;

	private $menu = null;
	private $roles = null;
	private $_current_menu = '';
	private $current_parent_menu_name = '';
	private $_current_parent_menu = '';

	/**
     * 构造函数
     *
     * @access  public
     * @return  void
     */
	public function __construct()
	{
		$this->CI = & get_instance();
		$this->CI->load->model('deploy/menu_model');
		$this->CI->load->library('session');
		$this->_filter_menus();
	}
	
	// ------------------------------------------------------------------------

    /**
     * 输出边栏菜单
     *
     * @access  public
     * @return  void
     */
	public function show_menu()
	{
		$rights = empty($this->roles) ? null : explode(',', $this->roles->rights);
		foreach ($this->menu as $key => $v)
		{
			if ($v['sub_menus'])
			{
				$i = 0;
				foreach ($v['sub_menus'] as $j)
				{
					if(!empty($rights) && !in_array($j['id'], $rights)) {
						$i++;
						continue;
					}
				}
				
				if(count($v['sub_menus']) == $i)
					unset($this->menu[$key]);
			}
		}
		
		foreach ($this->menu as $key => $v)
		{
			if ($v['sub_menus'])
			{
				$hasCur = "";
				$tempHtml =  '<a class="menuPrimary fontDinproMedium">' . $v['menu_name'] . '<li class="caretBottom"></li></a><ul class="menuList fontDinpro">';
				foreach ($v['sub_menus'] as $j)
				{
					if(!empty($rights) && !in_array($j['id'], $rights)) {
						// 						 		echo $j['id'].' ';
						continue;
					}

					$tempHtml = $tempHtml. '<li><a href="javascript:void(0);" link="'.backend_url($j['c_name'] . '/' . $j['m_name']) .'" class="' . (strcmp($this->_current_menu, $j['c_name'])==0 ? 'cur' : '') .'">'.$j['menu_name'].'</a></li>';
					if(strcmp($this->_current_menu, $j['c_name'])==0){
						$hasCur = "cur";
						$this->current_parent_menu_name = $v['menu_name'];	
						$this->_current_parent_menu = $key;	
					}
				}
				$tempHtml = '<div class="menuListGroup '.$hasCur.'">'.$tempHtml.'<li class="footEmpty"></li></ul></div>';
				echo $tempHtml;
			}
		}
	}

	// ------------------------------------------------------------------------
	public function show_menu_bar(){
		$rights = empty($this->roles) ? null : explode(',', $this->roles->rights);
		if(isset($this->menu[$this->_current_parent_menu])){
			foreach($this->menu[$this->_current_parent_menu]['sub_menus'] as $j)
			{
				if( !empty($rights) && !in_array($j['id'], $rights)){
					continue;
				}
				if(strcmp($this->_current_menu, $j['c_name']) == 0){
					$menu_bar_item = '<a href="javascript:void(0)" class="fontDinpro navBtn cur">'.$j['menu_name'].'</a>';
				}
				else{
					$menu_bar_item = '<a href="'.backend_url("{$j['c_name']}/{$j['m_name']}").'" class="fontDinpro navBtn">'.$j['menu_name'].'</a>';
				}
				echo $menu_bar_item;
			}
		}
	}

    /**
     * 过滤菜单
     *
     * @access  private
     * @return  void
     */
	private function _filter_menus()
	{
		$this->menu = $this->CI->menu_model->get();
		$this->_current_menu = $this->CI->uri->rsegment(1);
	}
	
	// ------------------------------------------------------------------------

    /**
     * 检测插件
     *
     * @access  public
     * @param   string
     * @return  void
     */
	public function permit($uid)
	{
		if(empty($uid))
			return false;
		
		if($uid == 1)
			return true;
		
		$rights = empty($this->roles) ? null : explode(',', $this->roles->rights);
		$id = $this->CI->menu_model->get_menu_id($this->_current_menu);
		return in_array($id, $rights);
	}

	// ------------------------------------------------------------------------

    /**
     * 设置顶部选中菜单
     *
     * @access  public
     * @param   int
     * @return  void
     */
	public function set_current_menu($key = 0)
	{
		$this->_current_menu = $key;
	}
	
	public function set_menu($menu)
	{
		$this->menu = $menu;
	}
	
	public function set_role($roles)
	{
		$this->roles = $roles;
	}
	public function get_current_parent_menu(){
		return $this->current_parent_menu_name;
	}	
}
	
/* End of file Acl.php */
/* Location: ./shared/libraries/Acl.php */
